package com.ganbo.rabbitmq.ttl;


import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
public class TtlController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/ttl")
    public String ttl() {

        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setExpiration("4000"); // 消息的过期属性，单位ms
        messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        Message message = new Message("这条消息4秒后过期".getBytes(), messageProperties);
        rabbitTemplate.send("GP_TTL_EXCHANGE", "gupao.ttl", message);

//        // 随队列的过期属性过期，单位ms
//        rabbitTemplate.convertAndSend("GP_TTL_EXCHANGE", "gupao.ttl", "这条消息");

        return "ok";
    }


    @GetMapping("/dl")
    public String dl() {

        MessageProperties messageProperties = new MessageProperties();
//        messageProperties.setExpiration("4000"); // 消息的过期属性，单位ms
//        messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        Message message = new Message("这条消息4秒后过期".getBytes(), messageProperties);
        rabbitTemplate.send("putongExchange", "#", message);
        return "ok";
    }


    @GetMapping("/defaultExchange")
    public String defaultExchange() {
        rabbitTemplate.convertAndSend("", "#", "这条消息=======================");
        return "ok";
    }
}
